package com.yanqu.road.server.cmd.activity.chuhan;

import com.yanqu.road.entity.enums.eTongBingStage;
import com.yanqu.road.logic.activity.chuhan.ChuHanConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChuHanProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tongbing.ChuHanMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_CHUHAN_RECRUIT_SOLDIER, desc = "")
public class ChuHanBarracksCallSoldierCmd extends AbstractCommand {

    private ChuHanProto.ChuHanRecruitSoldierReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = ChuHanProto.ChuHanRecruitSoldierReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ChuHanConfig config = ChuHanMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!config.inEnterActivityTime()) {
            ret = GameErrorCode.E_TONG_BING_NO_OPERATE_TIME;
            return;
        }
        // 活动结算时无法花钱招兵
        if (reqMsg.getType() == 1 && reqMsg.getSpeed() != 0L || reqMsg.getType() == 3) {
            if (config.getTongBingStage() == eTongBingStage.Stage1EndTime) {
                ret = GameErrorCode.E_CHUHAN_NO_OPERATE_TIME;
                return;
            }
        }

        long num = reqMsg.getNum();
        int recruitType = reqMsg.getType();
        long speed = reqMsg.getSpeed();

        // 请求跨服类型：1直接请求招兵，2请求招兵银两消耗
        int reqType = 1;
        if (recruitType == 1 && speed != 0) {
            reqType = 2;
            // 最大结束时间只能到最后一阶段结束
            long now = System.currentTimeMillis();
            List<List<Long>> stage1TimeList = config.getStage1TimeList();
            long leftSeconds = (stage1TimeList.get(stage1TimeList.size() - 1).get(1) - now) / 1000;
            num = Math.min(num, leftSeconds * speed);
        }
        ChuHanProto.CrossChuHanRecruitSoldierReqMsg.Builder builder = ChuHanProto.CrossChuHanRecruitSoldierReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReqType(reqType);
        builder.setType(reqMsg.getType());
        builder.setNum(num);
        builder.setSpeed(reqMsg.getSpeed());

        if (recruitType == 1 && speed == 0) { // 【取消招兵】

        } else if (recruitType == 1 && speed != 0) { // 【1银两招兵】
            Map<Long, Long> recruitSoldierMap = ChuHanMgr.getConfig().getRecruitSoldierMap();
            if (!recruitSoldierMap.containsKey(speed)) {
                ret = GameErrorCode.E_CHUHAN_BARRACKS_SPEED;
                return;
            }
        } else if (recruitType == 2) { // 【2医院救治】
            if (num <= 0) {
                ret = GameErrorCode.E_CHUHAN_PARAM_ERR;
                return;
            }
        } else if (recruitType == 3) { // 【3兵符招兵】
            if (num <= 0) {
                ret = GameErrorCode.E_CHUHAN_PARAM_ERR;
                return;
            }
            String cost = config.getBingFuItem() + "=" + num;
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(cost))) {
                ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                return;
            }
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(config.getBingFuItem());
            builder.setBingFuNum(goodsInfo.getParamList().get(0).longValue());
            player.getModule(PreReduceModule.class).preReduce(cost);
        } else {
            ret = GameErrorCode.E_CHUHAN_PARAM_ERR;
            return;
        }

        player.sendUnionActivityGroupPacket(CrossProtocol.C2_CHUHAN_CALL_SOLDIER, builder, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        ChuHanProto.ChuHanRecruitSoldierRespMsg.Builder respMsg = ChuHanProto.ChuHanRecruitSoldierRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(Protocol.S_CHUHAN_RECRUIT_SOLDIER % 20000, respMsg);
    }

}
